
# dict1 = data 数据导入dict1
def data_flatten(key,val,con_s='-',basic_types=(str,int,float,bool,complex,bytes)):

    if isinstance( val, dict):
        for ck, cv in val.items():
#             print(ck,cv)
            yield from data_flatten(con_s.join([key,ck]).lstrip('-'), cv)
    elif isinstance (val, (list,tuple,set)):
        for item in val:
            yield from data_flatten(key,item)
    elif isinstance(val, basic_types) or val is None:
        yield str(key).lower(),val
        
        
        
data = []
alldata = []
for k,v in enumerate(data_flatten('',dict1)):
    #print(k,v)
    data.append(v)
count = 0
for k,i in enumerate(data):
#     print(k,i)
    if  count <= count :
        alldata.append([])
        alldata[count].append(i)
        if i[0] == 'securitygroups-vpcid':
            count += 1
            
        
description = []
securitygroupname = []
securitygroupid = []
vpcid = []
protcol = []
egressprotcol = []
portrang = []
egressportrang = []
source = []
sourceegress = []
sourcedescription = []
sourceegressdescription = []
b = []
b1 = []
count = 0
count1 = 0
num = 0
length = len(dict1['SecurityGroups'])
test = True


# for i in range(100):
#     source.append(None)

# print(type(source))

for i in range(length):
    check = True
    portrang.append([])
    protcol.append([])
    egressportrang.append([])
    source.append([])
    sourceegress.append([])
    sourcedescription.append([])
    egressprotcol.append([])
    sourceegressdescription.append([])
    for j in alldata[i]:
        if j[0] == 'securitygroups-description':
            description.append(j[1])
        
        if j[0] == 'securitygroups-groupname':
            securitygroupname.append(j[1])
        if j[0] == 'securitygroups-groupid':
            securitygroupid.append(j[1])
        if j[0] == 'securitygroups-vpcid':
            vpcid.append(j[1])
        if j[0] == 'securitygroups-ippermissions-ipprotocol':
            protcol[i].append(j[1])

        if j[0] == 'securitygroups-ippermissionsegress-ipprotocol':
            egressprotcol[i].append(j[1])
            
        if 'egress' not in j[0] and 'fromport' in j[0] or 'egress' not in j[0] and 'toport' in j[0]:
            b.append(j[1])
            count += 1
            if count % 2 == 0:
#             print(b)
                a1 = b[0]
                a2 = b[1]
                a = "{}-{}".format(a1,a2)
                
                portrang[i].append(a)
                b = []  

        if 'egress-fromport' in j[0] or 'egress-toport' in j[0]:
#             print(j[1])
            b1.append(j[1])
            count1 += 1
            if count1 % 2 == 0:
#                 print(b1)
                a3 = b1[0]
                a4 = b1[1]
                a5 = "{}-{}".format(a3,a4)
                
                egressportrang[i].append(a5)
                b1 = []                  
                
                
                
        if 'securitygroups-ippermissions-ipranges-cidrip' == j[0] :
            source[i].append(j[1])
            
        if 'securitygroups-ippermissionsegress-ipranges-cidrip' == j[0]:
            sourceegress[i].append(j[1])
            
        if 'securitygroups-ippermissions-ipranges-description' == j[0]:
            sourcedescription[i].append(j[1])
            
        if 'securitygroups-ippermissionsegress-ipranges-description' == j[0]:
            sourceegressdescription[i].append(j[1])
            
            
        
        
# portrang,len(securitygroupname),securitygroupname,source[26],len(source)
# egressportrang,len(egressportrang)
# print(sourcedescription)
print("description securitygroupname securitygroupid vpcid  portrang protcol egressportrang egressprotcol source sourcedescription sourceegress sourceegressdescription  ")

for i in range(4):
    print(description[i], securitygroupname[i],securitygroupid[i],vpcid[i],portrang[i],protcol[i],egressportrang[i], egressprotcol[i], source[i],sourcedescription[i],sourceegress[i],sourceegressdescription[i])



